package com.xmut.web;

import com.sirdc.modules.core.exceptions.ServiceException;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.xmut.entity.Floor;
import com.xmut.filter.FloorFilter;
import com.xmut.service.FloorService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@Controller
@RequestMapping("/modules/xmut/floor")
public class FloorController extends JsonBaseController {

    @Autowired
    private FloorService floorService;

    @Override
    protected String getView(String view) {
        return String.format("/modules/xmut/floor/%s",view);
    }

    /**
     * 初始化界面
     *
     * @return url
     */
    @RequiresPermissions("modules:xmut:floor:query")
    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {
        return getView("query");
    }

    /**
     * 输出列表
     *
     * @param filter
     * @return json
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:floor:query")
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute FloorFilter filter) {
        List<Floor> floors = floorService.query(filter);
        return coverJqGrid(filter, floors);
    }

    /**
     * 跳转至filter界面
     * @param filter
     * @return
     */
    @RequestMapping("/filter")
    @RequiresPermissions("modules:xmut:floor:query")
    public String filter(@ModelAttribute FloorFilter filter) {
        return getView("filter");
    }

    /**
     * 执行创建
     * @param floor
     * @return
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:floor:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute Floor floor) {
        floorService.save(floor);
        return coverMessage("200");
    }

    /**
     * 初始化创建界面
     * @param model
     * @return url
     */
    @RequiresPermissions("modules:xmut:floor:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAddView(@ModelAttribute  Floor floor, Model model) {
        return getView("form");
    }

    /**
     * 初始化更新界面
     *
     * @param model
     * @param id
     * @return url
     */
    @RequiresPermissions("modules:xmut:floor:edit")
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable Long id) {
        Floor floor = floorService.getById(id);
        model.addAttribute("floor", floor);
        return getView("form");
    }

    /**
     * 更新操作
     * @param floor
     * @return
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:floor:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message update(@ModelAttribute Floor floor) {
        floorService.update(floor);
        return coverMessage("200");
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:floor:delete")
    @RequestMapping(value = "/delete/{id}")
    public Message delete(@PathVariable List<Long> id) {
        floorService.batchDelete(id);
        return coverMessage("200");
    }
}
